<?php
/*
SELECT id_question
FROM `questions`
WHERE id_competition =1
*/
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-type: application/json');
session_start();
require_once 'Mysql.php';
/* 
 * this file musn't be executed if the $_POST is not set 
 * to verfy that this script is excuted asynchronolsly call the index.php -> script.js -> NextQuestion() function
 * */
	//
if(isset($_POST['reponse_id'])){
	/* get the correct response and the response of the user */
	/* compare the the user response with the correct  one*/	
	$mysql = new Mysql();
	/* increment the score if the resopnse is correct  
	 * else do nothing (you can decrement if the canadiant systeme is on)
	 * */
	/* befor to test on the question if it's true or false we put the 
	 * answer into the user_question_reponse table
	 * */
//if("first_question" != $_POST['reponse_id']){ // c'est pas la pain de comparer les si on est dans l& 1er qustion
	$mysql->setResponse($_SESSION['user_id'],$mysql->getCountPointer($_SESSION['user_id']),$_POST['reponse_id']);
	if($_POST['reponse_id'] == $mysql->get_correct_response($mysql->getCountPointer($_SESSION['user_id']))){
		$mysql->incrementer_score($_SESSION['user_id']);
	}
//}

require_once 'constants.php';
mysql_connect(DB_SERVER,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);

/* get the previous choice of user and put it to the database*/
//$reponse = $_POST['reponse_id'];
 /* increment the session variable to point to the next question */
$mysql->incrementcount($_SESSION['user_id']); 
/* executer la requete avec la nouvelle variable */ 
/* TO DO merg $sql1 and $sal2 to one request */

$sql1 = mysql_query("SELECT question,image_path FROM questions WHERE id_question=".$mysql->getCountPointer($_SESSION['user_id']));
$la_question = mysql_fetch_array($sql1,MYSQL_BOTH);
$data["question"] = $la_question['question']; // add the question to the associative array
$data["image_path"] = $la_question['image_path'];

$sql2 = mysql_query("SELECT id_reponse,reponse FROM reponses WHERE id_question=".$mysql->getCountPointer($_SESSION['user_id']));
while ($row = mysql_fetch_assoc($sql2) ){
	$data[$row['id_reponse']] = $row['reponse'];
}
/* we can test if the request return none we send to the user a "end" string to
 * tell them that the QCM is end
 *  */
 /* increment the session variable to point to the next question */
 
//echo "<pre>";print_r($data); echo "</pre>";
$data["score"] = $mysql->get_score($_SESSION['user_id']);
/* transform the current question to an json foramt */
$arr = array($data);
/* sent the json file */
echo json_encode($arr);
}
else {
	echo json_encode(array("question" => "i_cant_bring_anything"));
}
/* if the user restart the session the server will ask for the pass again
 * then he must continu at the question that he stop
 * thats mean that the CurrentQuestion must be enregistrd in the database
 * the same thing applied to the score   
 * */
